<template>
  <div id="app">
    <el-card shadow="always">
      {{usersearch}}
<!--表单-->
      <el-form :inline="true" :model="usersearch" class="demo-form-inline">
        <el-form-item>
          <el-input v-model="usersearch.name" placeholder="学生姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="usersearch.age" placeholder="学生年龄"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button  type="primary" plain @click="search" icon="el-icon-search">查询</el-button>
          <el-form-item>
            <el-button type="success" plain @click="add" icon="el-icon-plus">添加学生</el-button>
          </el-form-item>
        </el-form-item>
      </el-form>

      <el-table
          :data="userList"
          style="width: 100%">
        <el-table-column
            label="学号"
            align="center"
            prop="id">
        </el-table-column>
        <el-table-column
            label="姓名"
            align="center"
            prop="name">
        </el-table-column>
        <el-table-column
            label="年龄"
            align="center"
            prop="age">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-card>
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
      {{TUser}}
      <el-form :model="TUser">
        <el-form-item label="学生姓名" :label-width="formLabelWidth">
          <el-input v-model="TUser.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="学生年龄" :label-width="formLabelWidth">
          <el-input v-model="TUser.age" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saves">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import userapi from "@api/user";

export default {
  data() {
    return {
      userList: [],      // 当前页的所有学生信息集合
      page: 1,               // 当前页
      pageSize: 5,           // 每页大小
      total: 100,            // 总记录数
      searchStudent:{},     // 要查找的学生信息对象
      usersearch: {},    // 代表查询条件对象
      classesList: [],
      dialogFormVisible:false,  // 控制对话框的显示隐藏
      formLabelWidth:'100px',   // 对话框提示文本的宽度
      TUser: {},              // 学生对象（添加或修改）
      title: '添加学生',
    }
  },

  created(){
    // this.findByPage(this.page,this.pageSize)
    this.search()
    // this.findUsers()


  },



  methods: {

    // findUsers(){
    //   this.$axios.get('TUser/list.do')
    //       .then(res =>{
    //         console.log(res)
    //         // this.userList=res.data;
    //         console.log("数据：",res.data)
    //         this.userList=res.data
    //       })
    // },

    // async findByPage(){
    //   // this.$axios.get(`/TUser/findByPage.do?page=${this.page}&pageSize=${this.pageSize}`)
    //   //     .then(res => {
    //   //       console.log("数据：",res)
    //   //       this.userList=res.data.rows
    //   //       this.total=res.data.total
    //   //
    //   //     })
    //
    //   const {data:res} = await userapi.findByPage(this.page,this.pageSize)
    //   this.userList = res.rows
    //   this.total = res.total
    //   },

    // 条件查询带分页
    async search(){
      
      const {data:res} = await userapi.search(this.page,this.pageSize,this.usersearch)
      this.userList = res.rows
      this.total = res.total
    },

    add(){
      this.dialogFormVisible=true;

    },

    async saves(){
      const {data:res} = await userapi.save(this.TUser)
      if (res.code === 200){
        this.$message({
          type: 'success',
          message: res.message
        })
        // 关闭对话框
        this.dialogFormVisible = false
        // 重新刷新页面
        this.search()
      }else {
        this.$message({
          type: 'error',
          message: res.message
        })
      }
    },

    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      // this.findByPage(this.page,this.pageSize)
      this.search()
    },
    // 当前页改变时事件
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      // this.findByPage(this.page,this.pageSize)
      this.search()
    }


  }
}
</script>

<style>
    el-card{
      width: 90%;
      margin: 0 auto;
    }

    el-pagination{
      text-align: right;
      margin-top: 15px;
    }


</style>
